package com.yhg.qxmall.socket.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhg.common.utils.PageUtils;
import com.yhg.common.utils.Query;

import com.yhg.qxmall.socket.dao.DailyDao;
import com.yhg.qxmall.socket.entity.DailyEntity;
import com.yhg.qxmall.socket.service.DailyService;


@Service("dailyService")
public class DailyServiceImpl extends ServiceImpl<DailyDao, DailyEntity> implements DailyService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DailyEntity> page = this.page(
                new Query<DailyEntity>().getPage(params),
                new QueryWrapper<DailyEntity>()
        );

        return new PageUtils(page);
    }
   //图表显示，返回两部分数据，日期json数组，数量json数组。日期
    @Override
    public Map<String, Object> getShowData(String area, String type, String begin, String end) {

        QueryWrapper<DailyEntity> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
//        wrapper.select("temperature","humidity","dan","lin","jia").like("area",area);
        wrapper.select("date_calculated",type).like("area",area);
        List<DailyEntity> dailyList = baseMapper.selectList(wrapper);

        //因为返回有两部分数据：日期 和 日期对应数量
        //前端要求数组json结构，对应后端java代码是list集合
        //创建两个list集合，一个日期list，一个数量list
        List<String> date_calculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();

        //遍历查询所有数据list集合，进行封装
        for (int i = 0; i < dailyList.size(); i++) {
            DailyEntity daily = dailyList.get(i);
            //封装日期list集合
            date_calculatedList.add(daily.getDateCalculated());
            //封装对应数量
            switch (type) {
                case "temperature":
                    numDataList.add(daily.getTemperature());
                    break;
                case "humidity":
                    numDataList.add(daily.getHumidity());
                    break;
                case "dan":
                    numDataList.add(daily.getDan());
                    break;
                case "lin":
                    numDataList.add(daily.getLin());
                    break;
                case "jia":
                    numDataList.add(daily.getJia());
                    break;
                default:
                    break;
            }
        }
        //把封装之后两个list集合放到map集合，进行返回
        Map<String, Object> map = new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("numDataList",numDataList);
        return map;
    }

}